<?php

/***************************************************************\
* FILE:		/sys/locationfunctions.inc
* SECTION:	util > location management functions
* FUNCTION:	reusable functions for location management
* USES:		sys/formfunctions.inc
* INCLUDES:	sys/inputcheck.inc
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

// TODO (mandragon): add region_id

include_once('sys/inputcheck.inc');

function location_check_address($address) {
	$ok = check_exists($address['street'], "straat") ? 1 : 0;
	$ok = (check_exists($address['street_number'], "straatnr") and $ok) ? 1 : 0;
	$ok = (check_exists($address['zip'], "postcode") and $ok) ? 1 : 0;
	$ok = (check_exists($address['city'], "woonplaats") and $ok) ? 1 : 0;
	$ok = (check_tags($address['street'], "straat") and $ok) ? 1 : 0;
	$ok = (check_chars($address['street_number'], "straatnr") and $ok) ? 1 : 0;
	$ok = (check_chars($address['zip'], "postcode") and $ok) ? 1 : 0;
	$ok = (check_tags($address['city'], "woonplaats") and $ok) ? 1 : 0;
	
	return $ok;
}

function location_add_address($address) {
	
	if (location_check_address($address)) {
		$sql = db_query_insert(array('street', 'street_number', 'zip', 'city'), array(db_sqlize($address['street']), db_sqlize($address['street_number']), db_sqlize($address['zip']), db_sqlize($address['city'])), "ADDRESS");
		db_do_query($sql);
		return db_insert_id();
	}
	
	return 0;
}

function location_edit_address($address) {
	if (location_check_address($address)) {
		$sql = db_query_update(array('street', 'street_number', 'zip', 'city'), array(db_sqlize($address['street']), db_sqlize($address['street_number']), db_sqlize($address['zip']), db_sqlize($address['city'])), "ADDRESS", "address_id = {$_POST['address_id']}");
		db_do_query($sql);
	}
}

function location_fetch_address($address_id) {
	$sql = db_query_select(array(array('*')), array('ADDRESS'), "address_id = $address_id");
	$sqlresult = db_do_query($sql);
	return db_fetch_array($sqlresult);
}

function location_fetch_location($location_id) {
	$sql = db_query_select(array(array('*')), array('ACTIVITY_LOCATION'), "activity_location_id = $location_id");
	$sqlresult = db_do_query($sql);
	return db_fetch_array($sqlresult);
}

function location_list_address() {
	$sql = db_query_select(array(array('*')), array('ADDRESS'), 1, "city ASC, street ASC");
	$sqlresult = db_do_query($sql);
	$ks = array();
	$vs = array();
	while ($r = db_fetch_array($sqlresult)) {
		array_unshift($ks, $r['address_id']);
		array_unshift($vs, "{$r['zip']} {$r['city']}, {$r['street']} {$r['street_number']}");
	}
	return array('keys' => $ks, 'values' => $vs);
}

function location_list_location() {
}

function location_form_address($default=array()) {
	$inputs = array();
	if ($default['address_id'])
		array_unshift($inputs, form_input_hidden("address_id", $default['address_id']));
	array_unshift($inputs, form_input_field("Straat", "street", $default['street'], 40, 90));
	array_unshift($inputs, form_input_field("Nr", "street_number", $default['street_number'], 5, 10));
	array_unshift($inputs, form_input_field("Postcode", "zip", $default['zip'], 7, 10));
	array_unshift($inputs, form_input_field("Woonplaats", "city", $default['city'], 30, 55));
	return form_create("addressform", $inputs);
}

function location_form_location($default=array()) {
	$inputs = array();
	if ($default['activity_location_id'])
		array_unshift($inputs, form_input_hidden("activity_location_id", $default['activity_location_id']));
	array_unshift($inputs, form_input_field("Naam", "location_name", $default['location_name'], 50, 60));
	array_unshift($inputs, form_input_text("Beschrijving", "description", $default['description']));
	$kv = location_list_address();
	$ks = $kv['keys'];
	$vs = $kv['values'];
	array_push($ks, "NEW");
	array_push($vs, "nieuw adres");
	array_unshift($inputs, form_input_select("Adres", "address_id", $ks, $vs, "kies een adres", $default['address_id'], "document.locationform.submit()"));
	
	if ($_POST['address_id'] == 'NEW') {
		$form = location_form_address();
		while($i = array_pop($form['contents']))
			array_unshift($inputs, $i);
	}
	array_unshift($inputs, form_input_text("Routebeschrijving", "route", $default['route']));
	array_unshift($inputs, form_input_field("URL naar prentje", "route_img", $default['route_img'], 80, 80));
	return form_create("locationform", $inputs);
}


?>